// C source file with some math-related function definitions

// TODOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOo

// int_powerof() function
// function to get the result of a given number to
// the power of a given positive integer number.
// Used mainly on the binary_to_decimal() and
// get_str_weight() functions.
//
// exponent (byte_t) --> exponent number
smax_t int_powerof(smax_t base, byte_t exponent)
{
  smax_t result = 1;
  for (smax_t i = 1; i <= exponent; i++)
    result *= base;  
  return result;
}


// char_array_to_binary() function
// function that takes a character array containing the bytes of an
// integer and converts each byte of it into its binary representation
//
// integer_size   (byte_t)    --> size of the integer to be converted in bytes
// integer_bytes  (byte_t *)  --> character array pointer containing the bytes of the integer
// integer_bits   (byte_t *)  --> character array pointer that will contain the bits calculated
//                            by this function
void char_array_to_binary(byte_t integer_size, byte_t * integer_bytes, byte_t * integer_bits)
{
  // loop through integer_bytes array
  for (smax_t i = 0; i < integer_size; i++)
  {
    // get current byte value from integer_bytes array
    byte_t currrent_int_byte = integer_bytes[i];
    byte_t current_arr_pos = i * 8;
    // do 8 loops to get the 8 bits of cur_var_byte
    for (smax_t j = 0; j < 8; j++)
    {
      // binary representation is calculated backwards so to put it forwards
      // on the array I have to visit the array elements from right to left
      integer_bits[(current_arr_pos + 7) - j] = currrent_int_byte % 2;
      currrent_int_byte = currrent_int_byte / 2;
    }
  }  
}


// binary_to_decimal() function
// function used to convert a character array containing 0s 
// and 1s into its decimal representation (signed or unsigned)
//
// binary_array_length  (byte_t)    --> length of the array containing the binary number
// binary_array         (byte_t *)  --> pointer to the array containing the binary number
// is_signed            (bool_t)  --> boolean to specify if integer is signed or not
smax_t binary_to_decimal(byte_t binary_array_length, byte_t * binary_array, bool_t is_signed)
{
  smax_t result = 0;  
  for (smax_t i = 0; i < binary_array_length; i++)
  { 
    // skip first element on variable_bin_rep
    // as the number might be signed/unsigned
    if (i == 0)
      continue;
    
    if (binary_array[i] == 1)
      result += int_powerof(2, (binary_array_length - 1) - i);
  }  
  // check is_signed and the first element on variable_bin_rep
  // and apply the respective operations to get the final number
  // and return result    
  if (is_signed == false && binary_array[0] == 0)
    return result;
  else if (is_signed == false && binary_array[0] == 1)
    return result + int_powerof(2, binary_array_length - 1);
  else if (is_signed == true  && binary_array[0] == 0)
    return result;
  else if (is_signed == true  && binary_array[0] == 1)
    return (result + 1) * (-1);    
  // if somehow none of the above conditions
  // are valid then return 0
  return 0;
}
